package com.example.opengldemo.vertex_magical;

import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.vertex_magical.core.FrameRenderer;
import com.example.opengldemo.vertex_magical.shader.OesRenderer;

public class FrameRendererImpl implements FrameRenderer {

    private FrameRenderer baseRenderer;
    private OesRenderer oesRenderer;

    public static final int TYPE_MOVE = 0;
    public static final int TYPE_CAMERA = 1;

    public FrameRendererImpl(FrameRenderer renderer) {
        this.baseRenderer = renderer;
        oesRenderer = new OesRenderer();

        setFlag(TYPE_MOVE);
    }

    public void setFlag(int flag) {
        if (flag == TYPE_MOVE) {
            oesRenderer.setVertexCo(new float[]{
                    -1.0f, 1.0f,0f ,
                    -1.0f, -1.0f,0f ,
                    1.0f, 1.0f, 0f ,
                    1.0f, -1.0f,0f
            });
        } else if (flag == TYPE_CAMERA) {
            oesRenderer.setVertexCo(new float[]{
                    -1.0f, -1.0f,0f ,
                    1.0f, -1.0f,0f ,
                    -1.0f, 1.0f,0f ,
                    1.0f, 1.0f,0f
            });
        }
    }

    public float[] getTextureMatrix() {
        return oesRenderer.getTextureMatrix();
    }

    @Override
    public void create() {
        MatrixState.setInitStack();
        oesRenderer.create();
        if (baseRenderer != null) {
            baseRenderer.create();
        }
    }

    @Override
    public void sizeChanged(int width, int height) {
        oesRenderer.sizeChanged(width, height);
        if (baseRenderer != null) {
            baseRenderer.sizeChanged(width, height);
        }
    }

    @Override
    public void draw(int texture) {
        if (baseRenderer != null) {
            baseRenderer.draw(oesRenderer.drawToTexture(texture));
        } else {
            oesRenderer.draw(texture);
        }
    }

    @Override
    public void destroy() {
        if (baseRenderer != null) {
            baseRenderer.destroy();
        }
        oesRenderer.destroy();
    }
}
